﻿#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Linq;
#endregion

namespace VisualWebGuiApplication2
{
    public partial class Usermanagerctrl : UserControl
    {
        user selected;
        public Usermanagerctrl()
        {
            InitializeComponent();
             comboBox1.DataSource = GetAccompanying_stocks();
             button1.Enabled = false;
        

        }







        public List<user> GetAccompanying_stocks()
            {
                List<user> _Accompanying_stocks = new List<user>();
               
                 using (GazOnDevEntities db = new GazOnDevEntities())
            {
                    var query = from p in db.users select p;
                    _Accompanying_stocks = query.ToList<user>();
               
            }
                return _Accompanying_stocks;
            }

        
            
     
         
        

        private void btnOpenTank_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("הוספת פריט למלאי נלווה");
            using (GazOnDevEntities db=new GazOnDevEntities())
            {
                user newrecord = new user();
                newrecord.lastname = txtNewlastname.Text;
                newrecord.firstname = txtNewFirstname.Text;
                newrecord.username = txtNewusername.Text;
                newrecord.password = txtNewpass.Text;
                newrecord.isadmin = chknewadmin.Checked;
                db.AddTousers(newrecord);
               int result = db.SaveChanges();
               if (result > 0)
               {
                   MessageBox.Show("  פעולה הצליחה  ");
                   btnOpenTank.Enabled = false;
               }
                
                
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // update  
            selected.lastname = txtEditlastname.Text;
            selected.firstname = txtEditFirstname.Text;
            selected.username = txtEditusername.Text;
            selected.password = txtEditpassword.Text;
            selected.isadmin = chknewadmin.Checked;
            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                db.Attach(selected);
                db.ObjectStateManager.ChangeObjectState(selected, EntityState.Modified);
                db.SaveChanges();
                MessageBox.Show("נשמר בהצלחה ");
            }
            button1.Enabled = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedValue != null)
            {

                selected = (user)this.comboBox1.SelectedItem;
                

                 txtEditlastname.Text=selected.lastname;
                 txtEditFirstname.Text=selected.firstname ;
              txtEditusername.Text=   selected.username;
               txtEditpassword.Text= selected.password;
                chkeditadmin.Checked=Convert.ToBoolean(selected.isadmin);
                button1.Enabled = true;
                // CurrentAmount = selected.Count;
                /*MessageBox.Show(selcted.Title);
                MessageBox.Show(selcted.Id.ToString());*/
            }

        }
    }
}